package cn.zqy.demo3.day02.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class Demo02C3p0 {
    public static void main(String[] args) {
        new Demo02C3p0().testDefalut();

        new Demo02C3p0().testNameConfig();


    }

    public void testDefalut() {
        // 1. 获取DataSource，使用默认配置
        DataSource ds = new ComboPooledDataSource();
        for (int i = 1; i < 12; ++i) {
            try {
                //2.获取连接
                Connection con = ds.getConnection();
                System.out.println(i + " : " + con);
                if (i == 5 ) {
                    con.close();  //归还连接到连接池中
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public void testNameConfig() {
        // 1.1 获取DataSource，使用指定名称配置
        DataSource ds = new ComboPooledDataSource("otherc3p0");
        for (int i = 1; i < 10; ++i) {
            try {
                //2.获取连接
                Connection con = ds.getConnection();
                System.out.println(i + " : " + con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
